package com.qilu.careprogram.controller;

import com.qilu.careprogram.pojo.MedicineInfo;
import com.qilu.careprogram.pojo.dto.ReviewInfoDTO;
import com.qilu.careprogram.pojo.ReviewInfo;
import com.qilu.careprogram.model.Result;
import com.qilu.careprogram.service.MedicineService;
import com.qilu.careprogram.service.ReviewService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/puwai")
public class PuwaiController {

    private static final Logger log = LoggerFactory.getLogger(PuwaiController.class);

    @Resource
    private ReviewService reviewService;
    @Resource
    private MedicineService medicineService;

    /**
     * 添加复查项目
     */
    @PostMapping("/addReviewInfo")
    public Result addReviewInfo(@RequestBody ReviewInfoDTO reviewInfoDTO) {
        try {
            reviewService.addReviewInfo(reviewInfoDTO);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error("添加复查项目失败: {}", e.getMessage());
            return Result.error("添加失败");
        }
    }

    /**
     * 获取复查项目列表
     */
    @GetMapping("/getReviewInfo")
    public Result<List<ReviewInfo>> getReviewInfo(@RequestParam String hospitalNumber) {
        try {
            List<ReviewInfo> reviewInfoList = reviewService.getReviewInfo(hospitalNumber);
            return Result.success(reviewInfoList);
        } catch (Exception e) {
            log.error("获取复查项目失败: {}", e.getMessage());
            return Result.error("获取失败");
        }
    }

    /**
     * 更新复查项目
     */
    @PostMapping("/updateReviewInfo")
    public Result updateReviewInfo(@RequestBody ReviewInfoDTO reviewInfoDTO) {
        try {
            reviewService.updateReviewInfo(reviewInfoDTO);
            return Result.success("更新成功");
        } catch (Exception e) {
            log.error("更新复查项目失败: {}", e.getMessage());
            return Result.error("更新失败");
        }
    }

    @GetMapping("/getMedicineInfo")
    public Result<List<MedicineInfo>> getMedicineInfo(@RequestParam String hospitalNumber,
                                                      @RequestParam Integer page,
                                                      @RequestParam Integer pageSize) {
        try {
            return Result.success(medicineService.getMedicineInfo(hospitalNumber,page,pageSize));
        } catch (Exception e) {
            log.error("获取服用药品失败: {}", e.getMessage());
            return Result.error("获取失败");
        }
    }

    @PostMapping("/addMedicineInfo")
    public Result addMedicineInfo(@RequestBody MedicineInfo medicineInfo) {
        try {
            medicineService.addMedicineInfo(medicineInfo);
            return Result.success("添加成功");
        } catch (Exception e) {
            log.error("添加服用药品失败: {}", e.getMessage());
            return Result.error("添加失败");
        }
    }
    @PostMapping("/updateMedicineInfo")
    public Result updateMedicineInfo(@RequestBody MedicineInfo medicineInfo) {
        try {
            medicineService.updateMedicineInfo(medicineInfo);
            return Result.success("修改成功");
        } catch (Exception e) {
            log.error("修改服用药品失败: {}", e.getMessage());
            return Result.error("修改失败");
        }
    }

    @PostMapping("/deleteMedicineInfo")
    public Result deleteMedicineInfo(@RequestBody MedicineInfo medicineInfo) {
        try {
            medicineService.deleteMedicineInfo(medicineInfo);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除服用药品失败: {}", e.getMessage());
            return Result.error("删除失败");
        }
    }
}